<?php
namespace Modules\Frontend\Controllers;

class UserTransferController extends \Phalcon\Mvc\Controller {
	public function beforeExecuteRoute() {
		\Components\Utils::isClosedFrontent($this);
	}

	public function initialize() {
		$this -> response -> setStatusCode(200, 'OK') -> sendHeaders() -> setContentType('application/json', 'UTF-8');
	}

	public function logAction($field) {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$query = $this -> request -> getQuery();
		if(!isset($query['page'])) {
			$query['page'] = 1;
		} else if($query['page'] <= 0)  {
			$query['page'] = 1;
		} else {
			$query['page'] = abs($query['page']);
		}

		if(!isset($query['limit'])) {
			$query['limit'] = 10;
		} else if($query['limit'] <= 0)  {
			$query['limit'] = 10;
		} else {
			$query['limit'] = abs($query['limit']);
		}

		$service = $this -> account::getWalletTransferLogServiceInstance() -> pager([
			'conditions' => '(account_wallet_transfer.from_user_id = ' . $serviceUser['user'] -> user_id . ' OR account_wallet_transfer.to_user_id = ' . $serviceUser['user'] -> user_id . ') AND account_wallet_transfer.field = ' . $field,
			'order' => 'account_wallet_transfer.id DESC'
		], $query['limit'], $query['page']);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'pager' => $service['pager'], 'current_user_id' => $serviceUser['user'] -> user_id]);
	}

	public function createAction() {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$post = $this -> request -> getPost();
		if($post['to_user_id'] <= 0) {
			return $this -> response -> setJsonContent(['code' => -1, 'msg' => '转账的用户有误!']);
		}

		if($post['to_user_id'] == $serviceUser['user'] -> user_id) {
			return $this -> response -> setJsonContent(['code' => -1, 'msg' => '不能转账给自己!']);
		}

		if($post['amount'] <= 0) {
			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>'数量不能少于0']);
		}

		if(!$this -> security -> checkHash($post['safePassword'], $serviceUser['user'] -> safe_password)) {
			return $this -> response -> setJsonContent(['code' => -1, 'msg' => '安全密码错误']);
		}

		$resToUser = $this -> account::getServiceInstance() -> findFirstById($post['to_user_id']);
		if($resToUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code' => $resToUser['code'], 'msg' => $resToUser['message']]);
		}

		if($resToUser['user'] -> pid == 0) {
			return $this -> response -> setJsonContent(['code' => -1, 'msg' => '此用户不是您的团队!']);
		}

		if($resToUser['user'] -> pid != $serviceUser['user'] -> user_id) {
			$currentParentToUser = $this -> account::getServiceInstance() -> findFirstById($resToUser['user'] -> pid);
			while(true) {
				if($currentParentToUser['code'] != 0) {
					return $this -> response -> setJsonContent(['code' => -1, 'msg' => '此用户不是您的团队!']);
				}

				if($currentParentToUser['user'] -> pid == $serviceUser['user'] -> user_id) {
					break;
				}

				if($currentParentToUser['user'] -> pid <= 0) {
					return $this -> response -> setJsonContent(['code' => -1, 'msg' => '此用户不是您的团队!']);
				}

				$currentParentToUser = $this -> account::getServiceInstance() -> findFirstById($currentParentToUser['user'] -> pid);
			}
		}

		$this -> db -> begin();
		try {
			$resTransfer = $this -> account::getWalletServiceInstance() -> transfer([
				'from_user_id' => $serviceUser['user'] -> user_id,
				'to_user_id' => $resToUser['user'] -> user_id,
				'amount' => $post['amount'],
				'field' => $post['field']
			]);
			if($resTransfer['code'] != 0) {
				throw new \Exception($resTransfer['message']);
			}

			$this -> db -> commit();
		} catch(\Exception $e) {
			$this -> db -> rollback();

			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>$e -> getMessage()]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'转出成功']);
	}
}